package manager
{
	import core.actor.core.ActorView;
	
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import map.GameMap;
	import map.MapInfo;
	
	import utils.ArrayUtil;

	public class CharManager
	{
		public var otherPlayerList:Vector.<ActorView>;
		public var characterList:Vector.<ActorView>//所有人物
		public var charCache:Dictionary = new Dictionary();//当characterList满时，用于储存没有显示的character
		
		public var gamePlayer:ActorView;//游戏主角
		
		//private var _characterListTotalNum:int = 0;
		
		private var _fighterNum:int = 20;
		private var _otherPlayerNum:int = 500;//其他玩家的人物列表
		private var _npcNum:int=20;//npc个数
		private var _totalNum:int = 1+_npcNum+_otherPlayerNum;
		
		private static var _instance:CharManager;
		
		private var _packsName:Array = ["m401t000","m402t000"//,"m405t000"
			,"m406t000","m407t000","m408t000","m409t000","m410t000"];
		
		public static function getInstance():CharManager{
			if(_instance == null){
				_instance = new CharManager();
			}
			return _instance;
		}
		
		public function CharManager()
		{
			initPlayer();
			initNpcList();
			initOtherPlayerList();
		}
		
		//初始游戏主角
		public function initPlayer():void{
			characterList = new Vector.<ActorView>(_totalNum);
			this.gamePlayer  = new ActorView();
			characterList[0] = gamePlayer;
			//initSortYList();
		}
		
		//初始化 30 个 NPC 
		public function initNpcList():void{
			for(var i:int=0;i<_npcNum;i++){
				characterList[i+1] = new ActorView();
			}
		}
		
		//初始化 其他玩家
		public function initOtherPlayerList():void
		{
			otherPlayerList  = new Vector.<ActorView>(_otherPlayerNum);
			for(var i:int=0;i<_otherPlayerNum;i++){
				var otherPlayer:ActorView = new ActorView();
				otherPlayer.typeId = ActorView.CHARACTER_OTHER_PLAYER;
				otherPlayerList[i]= otherPlayer;
				characterList[i+1+_npcNum] = otherPlayer;
			}
		}
		
		public function setGamePlayer(characterId:int, boneUrlStr:String):void{
			gamePlayer.cid = characterId;
			//gamePlayer.boneLayer = character;
			gamePlayer.initBone(_packsName);
			gamePlayer.typeId = ActorView.CHARACTER_PLAYER;
			gamePlayer.boneLayer.x = (MapInfo.view_width - MapInfo.chatWidth) / 2;
			gamePlayer.boneLayer.y = MapInfo.view_height / 2 - MapInfo.chatWidth;
			
			//gamePlayer.setAlternateCache(0);
			
			gamePlayer.x = gamePlayer.boneLayer.x;
			gamePlayer.y = gamePlayer.boneLayer.y;
			
			//gamePlayer.doAction("walk",Math.random()>0.5?true:false);
			gamePlayer.doAction("walk", true);
			
			SceneManager.getInstance().mainScene.gameMap.addLayerChild(gamePlayer.boneLayer, GameMap.ACTOR_LAYER);
		}
		
		/**
		 * 设置游戏主角坐标
		 * CursorManager->sceneMouseDownHandler ----> CharManager->setGamePlayerXY
		 * 1. 当满足以下条件时, 移动游戏主角
		 *   a. 游戏开始，即游戏状态为GameType.START
		 *   b. 鼠标处于默认状态 (默认状态？)
		 *   c. 玩家不处于交易状态(ON_TRADE_STATUS == false)
		 * 2. 如果点击的目标是NPC， 表示玩家将与NPC展开对话(需知道对话NPC的id), 先将玩家移动至NPC附近moveToNpc
		 * 3. 如果没有点击任何其他角色，且玩家处于非组队状态，则直接移动玩家到当前点击的点上
		 * @param event
		 * 
		 */		
		public function setGamePlayerXY(event:MouseEvent):void{
			var x:int = event.stageX + SceneManager.getInstance().mainScene.dx;
			var y:int = event.stageY  + SceneManager.getInstance().mainScene.dy;
			
			gamePlayer.moveTo(x,y);
		}
		
		//添加角色
		public function addCharacter(characterId:int, boneUrlStr:String, characterType:int):ActorView{
			var actor:ActorView = getCharacterByCid(characterId);
			if(actor==null){//characterList 中不存在该cid时加入
				for(var i:int=0;i<_totalNum;i++){
					actor = characterList[i];
					if( actor.cid==-1 && actor.typeId!=ActorView.CHARACTER_PLAYER){
						//actor.init(charData);
						actor.cid = characterId;
						//actor.boneLayer = character;
						actor.initBone(_packsName);
						
						//actor.setAlternateCache(0);
						actor.jumpFrame = true;
						
						actor.typeId = characterType;
						
						//actor.doAction("walk",Math.random()>0.5?true:false);
						actor.doAction("walk",true);
						
						SceneManager.getInstance().mainScene.gameMap.addLayerChild(actor.boneLayer, GameMap.ACTOR_LAYER);
						return actor;
					}
				}
				addCharCache(characterId, boneUrlStr); //charcterlist满了放到cache中
			}else{
				if(actor.typeId != ActorView.CHARACTER_PLAYER){//不是玩家主角
					
					SceneManager.getInstance().mainScene.gameMap.removeLayerChild(actor.boneLayer, GameMap.ACTOR_LAYER);
					
					actor.cid = characterId;
					//actor.boneLayer = character;
					actor.initBone(_packsName);
					
					//actor.setAlternateCache(0);
					actor.jumpFrame = true;
					
					actor.typeId = characterType;
					
					actor.doAction("walk",false);
					
					SceneManager.getInstance().mainScene.gameMap.addLayerChild(actor.boneLayer, GameMap.ACTOR_LAYER);
				}
			}
			return null;
		}
		
		//更新人物坐标
		public function updateCharacterXY(cid:int,x:int,y:int):void{
			//如果不是游戏主角 或 是游戏主角且为队伍成员时 更新位置
			//if(cid != -1 && cid!=gamePlayer.charData.cid || (cid == gamePlayer.charData.cid && GlobalState.GROUP_STATE==CharType.GROUP_MEMBER)){
				var actor:ActorView = characterList[cid];
				if(actor!=null){
					actor.moveTo(x, y);
				}
			//}
		}
		
		//移动以其玩家
		public function moveOtherPlayer():void{
			var actor:ActorView;
			for (var i:int = 0;i < _totalNum;i++){
				actor = this.characterList[i] as ActorView;
				if(null != actor && -1 != actor.cid){
					//其他玩家和自己是队伍的成员者移动
					if(actor.typeId == ActorView.CHARACTER_OTHER_PLAYER){// || actor.charData.typeId == CharType.CHARACTER_MEMBER){
						actor.move();
					}
					if(actor.typeId == ActorView.CHARACTER_PLAYER || actor.typeId == ActorView.CHARACTER_NPC || actor.typeId == ActorView.CHARACTER_OTHER_PLAYER){
						showOrHiddenChar(actor);
					}
				}
			}
			
			// 设置遮挡关系
			resetMainCharLayer();
		}
		
		//在场景的显示,不在的移除
		private function showOrHiddenChar(actor:ActorView):void{
			if (isCharInScene(actor)){
				ArrayUtil.updateCharacter(actor,actor.boneLayer.parent==null?true:false);
				
				//if(ActorView.CHARACTER_PLAYER != actor.typeId){
				//actor.boneLayer.x = actor.x - SceneManager.getInstance().mainScene.dx - actor.boneLayer.width/2;
				//actor.boneLayer.y = actor.y - SceneManager.getInstance().mainScene.dy;
				//}
			}else{
				//角色走出了屏幕的
				if(actor.boneLayer.parent != null){
					ArrayUtil.deleteArray(actor);
					
					SceneManager.getInstance().mainScene.gameMap.removeLayerChild(actor.boneLayer, GameMap.ACTOR_LAYER);
				}
			}
		}
		
		//角色是否在场景中
		private function isCharInScene(actor:ActorView):Boolean{
			var offsetX:int = actor.x - SceneManager.getInstance().mainScene.dx;
			var offsetY:int = actor.y - SceneManager.getInstance().mainScene.dy;
			
			if(offsetX < 0
				|| offsetX > MapInfo.view_width
				|| offsetY < 0
				|| offsetY > MapInfo.view_height)
				return false;
			
			return true;
		}
		
		public function moveAllCharacterWithOutPlayer():void{
			var actor:ActorView;
			for (var i:int = 0;i < _totalNum;i++){
				actor = this.characterList[i] as ActorView;
				if(null != actor && -1 != actor.cid && ActorView.CHARACTER_PLAYER != actor.typeId){
					//其他玩家和自己是队伍的成员者移动
					actor.boneLayer.x = actor.x - SceneManager.getInstance().mainScene.dx - actor.boneLayer.width/2;
					actor.boneLayer.y = actor.y - SceneManager.getInstance().mainScene.dy;
				}
			}
		}
		
		//mainScene场景中按照Y轴排序 按照Y轴顺序把重新放一下
		public function resetMainCharLayer():void{
			var actor:ActorView=null;
			for(var i:int=ArrayUtil.length-1;i>=0 ;i--){
				actor = ArrayUtil.showCharList[i];
				if(actor != null  && actor.cid != -1 ){
					SceneManager.getInstance().mainScene.gameMap.addLayerChild(actor.boneLayer, GameMap.ACTOR_LAYER);
				}
			}
		}
		
		//通过角色id找角色
		public function getCharacterByCid(cid:int):ActorView{
			var actor:ActorView = null;
			var list:Vector.<ActorView>=null;
			list=characterList;
			var len:int = list.length;
			for(var i:int=0;i<len;i++){
				actor = list[i];
				if( actor.cid==cid){
					return actor;
				}
			}
			return null;
		}
		
		//把显示不了的char放到缓存中
		public function addCharCache(characterId:int, boneUrlStr:String):void{
			charCache[characterId] = boneUrlStr;
		}
		
		//for test
		public function testOtherCharacterMove():void{
			//return;
			
			//for each(var actor:ActorView in otherPlayerList){
			for each(var actor:ActorView in characterList){
				if(null != actor && actor.isMoveEnd() && null != actor.boneLayer && ActorView.CHARACTER_PLAYER != actor.typeId)
				{
					//actor.moveTo(Math.random()*MapInfo.width,Math.random()*MapInfo.height);
					actor.moveTo(Math.random()*1000,Math.random()*560);
				}
			}
		}
	}
}